package com.weasel.weixin.commons.user;

import com.google.common.collect.ImmutableMap;
import com.weasel.http.HttpClientSecretary;
import com.weasel.http.helper.HttpResponseHelper;
import com.weasel.weixin.commons.CommonRequest;
import com.weasel.weixin.commons.CommonResponse;
import com.weasel.weixin.commons.config.CommonConfig;
import com.weasel.weixin.commons.config.CommonConsts;
import com.weasel.weixin.commons.user.bean.Group;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * Created by Dylan on 2015/10/20.
 */
public class UserGroupRequest extends CommonRequest {


    public UserGroupRequest(CommonConfig config) {
        super(config);
    }

    /**
     * 创建分组
     * @param groupName
     * @return
     * @throws IOException
     * @throws ExecutionException
     */
    public UserGroupResponse create(String groupName) throws IOException, ExecutionException {

        groupName = new String(groupName.getBytes("utf8"),"utf8");
        UserGroupResponse response = HttpClientSecretary.post()
                                                    .withURI(getBaseUrl(CommonConsts.CREATE_USER_GROUP_URL))
                                                    .withParam(ImmutableMap.of("group",ImmutableMap.of("name",groupName)))
                                                    .doItWithJson(UserGroupResponse.class);

        return response;
    }

    /**
     *
     * @return
     * @throws ExecutionException
     * @throws IOException
     */
    public UserGroupListResponse getList() throws ExecutionException, IOException {

        UserGroupListResponse response = HttpClientSecretary.get()
                                                        .withURI(CommonConsts.GET_USER_GROUP_LIST_URL)
                                                        .withParams(ImmutableMap.of("access_token", getAccessToken()))
                                                        .doItWithJson(UserGroupListResponse.class);

        return response;
    }

    /**
     *
     * @param openId
     * @return
     * @throws ExecutionException
     * @throws IOException
     */
    public int getUserGroupId(String openId) throws ExecutionException, IOException {

        int groupId = HttpClientSecretary.post()
                                         .withURI(getBaseUrl(CommonConsts.GET_USER_GROUP_ID_URL))
                                         .withParams(ImmutableMap.of("openid", openId))
                                         .doIt(new ResponseHandler<Integer>() {
                                             @Override
                                             public Integer handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

                                                 Map<String, Integer> _response = HttpResponseHelper.jsonResponseToEntity(response, Map.class);
                                                 return _response.get("groupid");
                                             }
                                         });
        return groupId;
    }

    public UserGroupResponse update(Group group) throws ExecutionException, IOException {

        UserGroupResponse response = HttpClientSecretary.post()
                                                    .withURI(getBaseUrl(CommonConsts.UPDATE_USER_GROUP_URL))
                                                    .withParam(ImmutableMap.of("group",group))
                                                    .doItWithJson(UserGroupResponse.class);
        return response;
    }

    /**
     *
     * @param openId
     * @param groupId
     * @return
     * @throws ExecutionException
     * @throws IOException
     */
    public CommonResponse moveUser(String openId,int groupId) throws ExecutionException, IOException {

        CommonResponse response = HttpClientSecretary.post()
                                                     .withURI(getBaseUrl(CommonConsts.MOVE_USER_TO_GROUP_URL))
                                                     .withParams(ImmutableMap.of("openid", openId, "to_groupid", groupId))
                                                     .doItWithJson(CommonResponse.class);
        return response;
    }

    /**
     *
     * @param openIds
     * @param groupId
     * @return
     * @throws ExecutionException
     * @throws IOException
     */
    public CommonResponse batchMoveUser(String [] openIds,int groupId) throws ExecutionException, IOException {

        CommonResponse response = HttpClientSecretary.post()
                                                     .withURI(getBaseUrl(CommonConsts.BATCH_MOVE_USER_TO_GROUP_URL))
                                                     .withParams(ImmutableMap.of("openid_list", openIds, "to_groupid", groupId))
                                                     .doItWithJson(CommonResponse.class);
        return response;
    }

    public CommonResponse delete(int groupId) throws ExecutionException, IOException {

        CommonResponse response = HttpClientSecretary.post()
                                                     .withURI(getBaseUrl(CommonConsts.DELETE_USER_GROUP_URL))
                                                     .withParam(ImmutableMap.of("group",ImmutableMap.of("id",groupId)))
                                                     .doItWithJson(CommonResponse.class);
        return response;
    }

}
